package com.swim.session;

import java.util.List;

import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;
import com.swim.entity.Skill;
import com.swim.entity.SkillRequest;

@Stateless
@RemoteBinding(jndiBinding="NewSkillRequestBeanJNDI")
public class NewSkillRequestBean implements NewSkillRequestRemote {

	@PersistenceContext(unitName = "SWIM")
	EntityManager database;
	
	/**
	 * @param from the RegisteredUser who sent an add request of a skill
	 * @param descSkill the description of the skil required
	 */
	public void newSkillRequest(String from, String descSkill) {
		RegisteredUserLocal r = getRegisteredUserLocal();
		SkillRequest req = new SkillRequest(descSkill, Util.getNow(), r.getUserWithEmail(from), r.getAdmin());
		database.persist(req);
	}

	/**
	 * @return the list of skill's request
	 */
	public List<SkillRequest> getSkillRequests() {
		try{
			Query q = database.createQuery("SELECT s FROM SkillRequest s");
			List<SkillRequest> result = (List<SkillRequest>) q.getResultList();
			return result;
	   	} catch (EntityNotFoundException exc) { 
			System.err.println("UFF1");
		} catch (javax.persistence.NoResultException exc) { 
			System.err.println("UFF2");
		} catch (NonUniqueResultException exc) {
			System.err.println("UFF3");
		}
		return null;
	}

	/**
	 * @param id the id of the request's skill
	 * @return the request of skill selected by the id
	 */
	public SkillRequest getSkillRequest(int id) {
		try{
			Query q = database.createQuery("SELECT s FROM SkillRequest s WHERE s.id = :id");
			q.setParameter("id", id);
			SkillRequest result =  (SkillRequest)q.getSingleResult();
			return result;
	   	} catch (EntityNotFoundException exc) { 
			System.err.println("UFF1");
		} catch (javax.persistence.NoResultException exc) { 
			System.err.println("UFF2");
		} catch (NonUniqueResultException exc) {
			System.err.println("UFF3");
		}
		return null;
	}
	
	/**
	 * @return the local interface of registered user
	 */
	private RegisteredUserLocal getRegisteredUserLocal() {
		InitialContext jndiContext;
		Object ref = null;
		try {
			jndiContext = new InitialContext();
			ref = jndiContext.lookup("RegisteredUserBeanLocalJNDI");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (RegisteredUserLocal) ref;
	}
	
	/**
	 * @param reqId the id of the skill'request
	 * @param accept says if the request is accepted
	 * @param name the name of the new skill
	 * @param desc the description of the new skill
	 */
	public void acceptNewSkillRequest(int reqId, boolean accept, String name, String desc) {
		SkillRequest req = getSkillRequest(reqId);
		Skill s = new Skill(name, desc);
		if(accept) {
			database.persist(s);
		}
		database.remove(req);
	}
}
